/*
 * 
 * 
 * 
 */
package com.cms.controller.admin;

import com.cms.constant.CommonConstant;
import com.cms.domain.Feedback;
import com.cms.entity.Admin;
import com.cms.service.AdminService;
import com.cms.utils.WebUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * Controller - 管理员登录
 * 
 * 
 * 
 */
@Controller("adminLoginController")
@RequestMapping("/admin/login")
public class LoginController extends BaseController {

    @Autowired
    private AdminService adminService;

    /**
     * 登录
     */
    @RequestMapping
    public String index(String username, String password, ModelMap modelMap) {
        modelMap.addAttribute("username",username);
        modelMap.addAttribute("password",password);
        return getView("login/index");
    }
    
    /**
     * 登录
     */
    @ResponseBody
    @RequestMapping("/login")
    public Feedback login(String username, String password){
        Admin admin = adminService.findByUsername(username);
        if(admin==null){
            return Feedback.error("用户名不存在!");
        }
    	if(!admin.getPassword().equals(DigestUtils.md5Hex(password))){
            return Feedback.error("用户名密码错误!");
        }
        WebUtils.getSession().setAttribute(CommonConstant.SESSION_ADMIN, admin);
        return Feedback.success(new HashMap<>());
    }
	

}